package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;

import com.example.demo.pojo.WorkUser;
import com.example.demo.server.ClassServer;
import com.example.demo.server.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserServer userServer;
    @CrossOrigin
    @PostMapping(value = "api/getUserList")
    @ResponseBody
    List<WorkUser> findAllUser(@RequestBody JSONObject jsonObject){
        int index=jsonObject.getIntValue("index")-1;
        String key=jsonObject.getString("key");
        return userServer.getUserList(index,key);
    }
    @CrossOrigin
    @PostMapping(value = "api/getUserNum")
    @ResponseBody
    int getUserNum(@RequestBody JSONObject jsonObject){
        String key=jsonObject.getString("key");
        return userServer.countUser(key);
    }
    @CrossOrigin
    @PostMapping(value = "api/addUserList")
    @ResponseBody
    int addUserList(@RequestParam("file") MultipartFile file) {
        return  userServer.uploadUserList(file);

    }
    @CrossOrigin
    @PostMapping(value = "api/delUser")
    @ResponseBody
    int delUser(@RequestBody JSONObject jsonObject) {
        String id=jsonObject.getString("id");
        return  userServer.deleteUser(id);

    }
    @CrossOrigin
    @PostMapping(value = "api/updateUser")
    @ResponseBody
    int updateUser(@RequestBody JSONObject jsonObject) {
        String id=jsonObject.getString("id");
        WorkUser workUser=jsonObject.getObject("user",WorkUser.class);
        return userServer.editUser(id,workUser);
    }
    @CrossOrigin
    @PostMapping(value = "api/resetPwd")
    @ResponseBody
    int resetPwd(@RequestBody JSONObject jsonObject) {
        String email=jsonObject.getString("email");
        String pwd=jsonObject.getString("pwd");
        return userServer.resetPwd(email,pwd);
    }

}
